package com.qeegoo.b2bautozityp.moudle.detail.view;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.moudle.detail.bean.DataSet;
import com.qeegoo.b2bautozityp.moudle.detail.bean.DetailBean;
import com.qeegoo.b2bautozityp.moudle.detail.adapter.LeftAdapter;
import com.qeegoo.b2bautozityp.moudle.detail.adapter.RightAdapter;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class LookForDetailFragment extends Fragment implements AdapterView.OnItemClickListener, DataSet<List<DetailBean.GoodsInfoMoreListBean>> {


    private ListView lv_left;
    private LeftAdapter leftAdapter;
    private List<DetailBean.GoodsInfoMoreListBean> goodsInfoMoreList;
    private TextView tv_name;
    private TextView tv_oem;
    private TextView tv_amount;
    private TextView tv_desc;
    private ImageView iv_pic_one;
    private ImageView iv_pic_two;
    private ImageView iv_pic_three;
    private ListView lv_right;
    private RightAdapter rightAdapter = new RightAdapter();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflateView = inflater.inflate(R.layout.fragment_look_for_detail, container, false);
        initWidget(inflateView);
        return inflateView;
    }


    private void initWidget(View inflateView) {
        lv_left = (ListView) inflateView.findViewById(R.id.lv_left);
        lv_right = (ListView) inflateView.findViewById(R.id.lv_right);
        View headerView = View.inflate(getActivity(), R.layout.header_right, null);
        tv_name = (TextView) headerView.findViewById(R.id.tv_name);
        tv_oem = (TextView) headerView.findViewById(R.id.tv_oem);
        tv_amount = (TextView) headerView.findViewById(R.id.tv_amount);
        tv_desc = (TextView) headerView.findViewById(R.id.tv_desc);
        iv_pic_one = (ImageView) headerView.findViewById(R.id.iv_pic_one);
        iv_pic_two = (ImageView) headerView.findViewById(R.id.iv_pic_two);
        iv_pic_three = (ImageView) headerView.findViewById(R.id.iv_pic_three);
        lv_right.addHeaderView(headerView);

    }

    /**
     * 加载数据入口
     *
     * @param goodsInfoMoreList
     */

    @Override
    public void setData(List<DetailBean.GoodsInfoMoreListBean> goodsInfoMoreList) {
        this.goodsInfoMoreList = goodsInfoMoreList;
        leftAdapter = new LeftAdapter(null, goodsInfoMoreList);
        lv_left.setAdapter(leftAdapter);
        lv_right.setAdapter(rightAdapter);
        lv_left.setOnItemClickListener(this);
        setChooseData(0);
    }

    private void setChooseData(int position) {

        DetailBean.GoodsInfoMoreListBean bean = goodsInfoMoreList.get(position);
        //设置头布局数据
        tv_name.setText(bean.getProductName());
        tv_amount.setText("数量: " + bean.getAmount());
        tv_desc.setText("备注: " + bean.getNote());
        Glide.with(getActivity()).load(bean.getAskImage1()).centerCrop().into(iv_pic_one);
        Glide.with(getActivity()).load(bean.getAskImage2()).centerCrop().into(iv_pic_two);
        Glide.with(getActivity()).load(bean.getAskImage3()).centerCrop().into(iv_pic_three);
        rightAdapter.setDataAndRefreshed(bean.getAskPriceQuoteGoodsList());
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        leftAdapter.setCurrentPositionAndRefreshed(position);
        setChooseData(position);
    }
}
